package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.core.domain.AjaxResult;
import com.campus.mapper.CommentMapper;
import com.campus.pojos.domain.ClientArticle;
import com.campus.pojos.domain.ClientComment;
import com.campus.pojos.domain.dto.CommentDto;
import com.campus.pojos.domain.dto.CommentQueryDto;
import com.campus.pojos.domain.vo.CommentVo;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.service.ArticleService;
import com.campus.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, ClientComment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    private ArticleService articleService;
    /**
     * 查询帖子评论
     * @param id
     * @return
     */
    public Result<List<CommentVo>> getRoot(Integer id) {
        //1.参数验证
        ClientArticle article = articleService.getById(id);
        if(article == null){
            return Result.error("帖子不存在!");
        }

        //2.根据帖子id获取相关的评论信息
        QueryWrapper<ClientComment> qw = new QueryWrapper<>();
        qw.eq("article_id", id);
        qw.eq("root_id", 0);
        List<ClientComment> list = list(qw);

        //3.分装返回结果
        List<CommentVo> resList = getCommentVos(list);

        return Result.success(resList);
    }

    private static List<CommentVo> getCommentVos(List<ClientComment> list) {
        List<CommentVo> resList = new ArrayList<>();
        for(ClientComment comment : list){
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(comment, commentVo);
            resList.add(commentVo);
        }
        return resList;
    }

    /**
     * 删除帖子评论
     * @param id
     * @return
     */
    public void deleteById(Integer id) {
        //1.删除该评论的子评论
        //2.删除所有回复它的评论
        QueryWrapper<ClientComment> qw = new QueryWrapper<>();
        qw.eq("root_id", id).or().eq("parent_id", id);
        remove(qw);

        //3.删除本条评论
        commentMapper.deleteById(id);
    }

    /**
     * 添加评论
     * @param commentDto
     * @return
     */
    public void add(CommentDto commentDto) {
        ClientComment clientComment = new ClientComment();
        BeanUtils.copyProperties(commentDto,clientComment);

        if(commentDto.getRootId() == null) {
            clientComment.setRootId(0L);
        }
        if(commentDto.getParentId() == null) {
            clientComment.setParentId(0L);
        }
        clientComment.setCommentContext(commentDto.getContent());
        clientComment.setCreateTime(LocalDateTime.now());
        clientComment.setUpdateTime(LocalDateTime.now());
        save(clientComment);
    }

    /**
     * 获取子评论
     *
     * @param rootId
     * @return
     */
    @Override
    public Result<List<CommentVo>> getSon(Integer rootId) {
        //1.参数验证
        ClientComment comment = getById(rootId);
        if(comment == null) {
            return Result.error("根id错误，未找到改评论");
        }
        //2.查询子评论
        QueryWrapper<ClientComment> qw = new QueryWrapper<>();
        qw.eq("root_id", rootId);
        List<ClientComment> list = list(qw);

        //3.分装返回结果
        List<CommentVo> commentVos = getCommentVos(list);

        return Result.success(commentVos);
    }

    /**
     * 评论条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getList(CommentQueryDto dto) {
        //1页码
        if(dto.getPage() == null ) dto.setPage(1);
        if(dto.getPageSize() == null) dto.setPageSize(10);
        IPage<ClientComment> page = new Page<>(dto.getPage(), dto.getPageSize());
        //2条件
        QueryWrapper<ClientComment> qw = new QueryWrapper<>();
        //2.1搜索框
        if(!StringUtils.isBlank(dto.getKeyword())){
            qw.like("comment_context", dto.getKeyword());
        }
        //2.2时间
        if(dto.getEndTime()!= null && dto.getBeginTime() != null){
            qw.ge("create_time", dto.getBeginTime());
            qw.le("create_time", dto.getEndTime());
        }
        //2.3 指定帖子下的评论
        if(dto.getArticleId() != null){
            qw.eq("article_id", dto.getArticleId());
        }
        page = commentMapper.selectPage(page, qw);
        //3 封装返回
        List<ClientComment> records = page.getRecords();
        ArrayList<CommentVo> commentVos = new ArrayList<>();

        for (ClientComment record : records) {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(record, commentVo);
            commentVos.add(commentVo);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(commentVos);
        return AjaxResult.success("success",pageResult);
    }

    /**
     * 删除评论
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        QueryWrapper<ClientComment> qw = new QueryWrapper<>();
        qw.eq("id", id)
                .or().eq("root_id", id)
                .or().eq("parent_id",id);
        remove(qw);
    }
}
